//
//  ClassifyCollectionReusableView.m
//  14-CollectionViewTest
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ClassifyCollectionReusableView.h"
#import "ClassifyCollectionViewCell.h"
#define kCollectionViewClassifyReuse @"classifyReuse"
@interface ClassifyCollectionReusableView ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *classifyCollection;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSMutableArray *arr_data;
@end

@implementation ClassifyCollectionReusableView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 创建label
        _label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, frame.size.width, 50)];
        _label.text = @"分类";
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _classifyCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(frame), CGRectGetHeight(frame) - 50) collectionViewLayout:layout];
        
        layout.itemSize = CGSizeMake((CGRectGetWidth(frame) - 30) / 2, (CGRectGetHeight(frame) - 40 - 50) / 3);
        // 设置行间距
        layout.minimumLineSpacing = 10;
        // 设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        // 设置上下左右边距
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        // 签订代理
        _classifyCollection.delegate = self;
        _classifyCollection.dataSource = self;
        _classifyCollection.backgroundColor = [UIColor whiteColor];
        // 注册cell
        [_classifyCollection registerClass:[ClassifyCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewClassifyReuse];
        // 添加到视图上
        [self addSubview:_classifyCollection];
        [self addSubview:_label];
        [self handleData];
    }
    return self;
}
- (void)handleData {
    self.arr_data = [NSMutableArray array];
    for (int i = 0; i < 12; i++) {
        NSString *name = [NSString stringWithFormat:@"hzw%d", i];
        UIImage *image = [UIImage imageNamed:name];
        [_arr_data addObject:image];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arr_data.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewClassifyReuse forIndexPath:indexPath];
    cell.imageView.image = _arr_data[indexPath.row];
    cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}







@end
